home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / NeXTSTEPAdvantage / Plotter / PlotController.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  109 lines

  1.  
  2. /* 
  3.  * PlotController.m -- Implementation file for the PlotController class 
  4.  *
  5.  * You may freely copy, distribute, and reuse the code in this example.
  6.  * NeXT disclaims any warranty of any kind, expressed or implied, as to its
  7.  * fitness for any particular use.
  8.  *
  9.  */
  10.  
  11. #import "PlotController.h"
  12. #import "PlotView.h"
  13. #import <appkit/Application.h>
  14. #import <appkit/Listener.h>
  15. #import <appkit/Pasteboard.h>
  16. #import <appkit/Window.h>
  17. #import <appkit/ScrollView.h>
  18. #import <appkit/Text.h>
  19. #import <stdlib.h> 
  20. #import <string.h> 
  21. #include <mach.h>
  22.  
  23. @implementation PlotController
  24.  
  25. - appDidInit:sender 
  26. /*
  27.  * Responds to a message that's sent just before the application
  28.  * gets the first event.  PlotController initializes its inputText
  29.  * instance variable.  It makes itself the inputText's delegate and
  30.  * the services delegate for the application.  It also ensures that 
  31.  * the application's single window becomes the key window.
  32.  */
  33. {
  34.     inputText = [theScrollView docView];
  35.     [inputText setDelegate:self];
  36.     [[NXApp appListener] setServicesDelegate:self];
  37.     [[thePlotView window] makeKeyAndOrderFront:self]; 
  38.     return self;
  39. }
  40.  
  41. - textDidGetKeys:theText isEmpty:(BOOL)flag
  42. /*
  43.  * Responds to a message the Text object sends when its text changes.
  44.  * PlotController causes the PlotView to clear itself when the text changes.
  45.  */
  46. {
  47.     return [thePlotView clear:self];
  48. }
  49.  
  50. - requestPlot:(id)pasteboard userData:(const char *)userData error: (char **)msg
  51. /*
  52.  * Responds to a request from another application for the plotting service.
  53.  * PlotController copies the data from the supplied pasteboard into the Text object
  54.  * and then sends a plot: message to the PlotView.
  55.  */
  56. {
  57.     char    *data, *scratch;
  58.     int     length;
  59.  
  60.     [NXApp activateSelf:NO]; 
  61.     [pasteboard types];
  62.     if ([pasteboard readType:NXAsciiPboardType data:&data length:&length]) {
  63.         if(scratch = malloc(length+1)) {
  64.             strncpy(scratch, data, length);
  65.             scratch[length]='\0';
  66.             [[inputText selectAll:self] replaceSel:scratch];
  67.             free(scratch);
  68.         }
  69.         vm_deallocate(task_self(), (vm_address_t)data, (vm_size_t)length);
  70.         [thePlotView plot:self];
  71.     }
  72.     return self;
  73. }
  74.  
  75.  
  76. - plotView:sender providePoints:(NXStream **)stream
  77. /*
  78.  * Responds to a message the PlotView sends requesting the points to plot.  
  79.  * PlotController responds by giving the PlotView access to the Text 
  80.  * object's stream.
  81.  */
  82. {
  83.     *stream = [inputText stream];
  84.     return self;
  85. }
  86.  
  87. - plotView:sender pointDidChange:(NXPoint *)aPoint
  88. /*
  89.  * Responds to a message the PlotView sends notifying its delegate 
  90.  * that a point has been added.  PlotController responds by adding the point to
  91.  * the end of the list in the Text object.
  92.  */
  93. {
  94.     int    endPos;
  95.     char    buffer[100];
  96.  
  97.     sprintf(buffer, "%d, %d\n", (int)aPoint->x, (int)aPoint->y);
  98.     endPos = [inputText byteLength];
  99.     [inputText setSel:endPos :endPos];
  100.     [inputText scrollSelToVisible];
  101.     [inputText replaceSel:buffer];
  102.     return self;
  103. }
  104.  
  105. @end
  106.  
  107.  
  108.  
  109.